//
//  XTStrategyDetailViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTStrategyDetailViewController.h"
#import "XTStrategyViewController.h"
#import "XTStrategy.h"
#import "NetworkHandle.h"
#import "UIImageView+WebCache.h"
#import "XTCollectionViewPhotos.h"
#import "XTStrategyCollectionViewCell.h"
#import "XTStrategyAlbumViewController.h"
@interface XTStrategyDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) UIAlertView *alertView;
@end

@implementation XTStrategyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"灵感集详情";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang1"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)] autorelease];

    [self createCollection];
}
- (void)rightAction
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //添加文件夹
    //拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    //创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"myStrategy.plist"];
    if (![fileManager fileExistsAtPath:strPath]) {
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:self.strategy];
        [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
        self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
        [_alertView show];
    } else {
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
        NSMutableArray *newArr = [NSMutableArray array];
        for (XTStrategy *strate in arr) {
            [newArr addObject: strate.title];
        }
        if (![newArr containsObject:self.strategy.title]) {
            [arr addObject:self.strategy];
            [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [_alertView show];
        } else {
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [_alertView show];
        }
    }
}
- (void)dismissAlertView
{
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)createCollection
{
    // 自适应
    CGFloat f1 = [[self class] heightWithText:self.strategy.title];
    CGFloat f2 = [[self class]heightWithText:self.name];
    CGFloat f3 = [[self class]heightWithText:self.strategy.sDescription];

    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, - f1 - f2 - f3 - 160, 375, 350)];
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = self.strategy.title;
    label1.numberOfLines = 0;
    [label1 setFont:[UIFont systemFontOfSize:18]];
    label1.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, f1 + 40);
    [self.myView addSubview:label1];
    [label1 release];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = self.name;
    label2.numberOfLines = 0;
    [label2 setFont:[UIFont systemFontOfSize:12]];
    label2.frame = CGRectMake(80, f1 + 50, 80, f2 + 50);
    [self.myView addSubview:label2];
    [label2 release];
    
    UILabel *label3 = [[UILabel alloc]init];
    label3.text = self.strategy.sDescription;
    label3.numberOfLines = 0;
    label3.frame = CGRectMake(10, 120, self.view.frame.size.width - 20, f3 + 70);
    [self.myView addSubview:label3];
    [label3 release];
    
    UIImageView *imageView1 = [[UIImageView alloc]init];
    NSString *str = [[self.strategy.user objectForKey:@"userImage"] objectForKey:@"small"];
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:str]placeholderImage:[UIImage imageNamed:@"3-1"]];
    imageView1.frame = CGRectMake(10, f1 + 50, 60, 60);
    imageView1.layer.masksToBounds = YES;
    [imageView1.layer setCornerRadius:30];
    [self.myView addSubview:imageView1];
    [imageView1 release];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.height / 5 - 20);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
//    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView addSubview:_myView];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [flowLayout release];
    [_collectionView registerClass:[XTStrategyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    // collectionView上方留位置
    _collectionView.contentInset = UIEdgeInsetsMake(f1 + f2 + f3 + 160, 0, 113, 0);
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _s2.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XTStrategyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSString *string = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w640_h0_m0.jpg", [self.s2[indexPath.row] objectForKey:@"id"]];
    XTCollectionViewPhotos *p = [[XTCollectionViewPhotos alloc] init];
    p.url = string;
    cell.photo = p;

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XTStrategyAlbumViewController *saVC = [[[XTStrategyAlbumViewController alloc] init] autorelease];
    saVC.photoArr = self.s2;
    saVC.page = indexPath.row;
    [self presentViewController:saVC animated:YES completion:^{
        
    }];
}
+ (CGFloat)heightWithText:(NSString *)text
{
    //设置一个字典保存文本属性
    NSDictionary *dic =[ NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    //预设一个尺寸  文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(375, 1000);
    //根据文本信息获得一个CGRect
    //参数1: 尺寸范围
    //参数2: 文本属性
    //参数3: 按照什么方式获取Rect
    //参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的高度
    return rect.size.height;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
